﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using NHibernate.Mapping;
using VitCode.Castle.DataAccess;


namespace VitCode.Castle.Management
{
    public class BaseDataManager : BaseDataAccess // 业务逻辑的帮助类
    {
       

        public bool DeleteObjById(object obj, string id)
        {
            BeginTransaction();
            try
            {
                IList lstObj = GetObjectList(obj.GetType().Name, "Id", id.Trim());
                if (lstObj != null && lstObj.Count > 0)
                {

                    Delete(lstObj[0]);
                    Commit();
                    return true;
                }
            }
            catch (Exception ex)
            {
                RollBack();
                return false;

            }
            finally
            {
                CloseSession();
            }
            return true;
        }

        public  bool SaveOrUpdateObj(object obj)
        {
            try
            {
                BeginTransaction();
                SaveOrUpdate(obj);
                Commit();
                return true;
            }
            catch (Exception ex)
            {
                RollBack();
                return false;

            }
            finally
            {
                CloseSession();
            }
           
        }

        public  void DeleteObj(object obj)
        {
            try
            {
                BeginTransaction();
                Delete(obj);
                Commit();

            }
            catch (Exception ex)
            {
                RollBack();


            }
            finally
            {
                CloseSession();
            }

        }



        public IList CheckExist(string hql)
        {
            return Session.CreateQuery(hql).List();
        }

        /// <summary>
        /// 使用存储过程  
        /// </summary>
        /// <param name="procedureName"></param>
        /// <param name="prefix"></param>
        /// <param name="isEnableDate"></param>
        /// <returns></returns>
        public string GetValueRunProc(string procedureName, string prefix, bool isEnableDate)
        {

            IDbCommand comm = Session.Connection.CreateCommand();
            if (Session.Transaction != null)
                Session.Transaction.Enlist(comm);
           
            comm.CommandText = procedureName;
            comm.CommandType = CommandType.StoredProcedure;
            IDbDataParameter parameter = comm.CreateParameter();
            parameter.ParameterName = "@prefix"; // driver.FormatNameForSql( info.Name ); 
            parameter.Value = prefix;
            comm.Parameters.Add(parameter);
            IDbDataParameter parameter2 = comm.CreateParameter();
            parameter2.ParameterName = "@isEnableDate";
            parameter2.Value = isEnableDate;
            comm.Parameters.Add(parameter2);
            string strBarcode = (string)comm.ExecuteScalar();
            return strBarcode;   
        }

        public bool SelectAreaById(string AreaId,out  StringCollection scMsg)
        {
            scMsg = new StringCollection();
            string strSql = "select Name from AmsCore_Site where AreaId='" + AreaId + "'";
            DataTable dt = GetDataTableBySql(strSql);
            if (dt != null && dt.Rows.Count > 0)
            {
                string areaName = GetAreaNameById(AreaId);
                for(int i=0;i<dt.Rows.Count;i++)
                {
                     scMsg.Add("区域:【" + areaName + "】下有地点【" + dt.Rows[i]["Name"] + "】不能删除!");
                }
                return true;
            }

            return false;
        }

        private string GetAreaNameById(string areaId)
        {
            var strSql = "select Name from AmsCore_Area where Id='" + areaId + "'";
            var dt = GetDataTableBySql(strSql);
            if(dt != null && dt.Rows.Count > 0)
            {
                return dt.Rows[0]["Name"].ToString();
            }
            return "";
        }
    }
}
